package com.iepacj.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by iEpac on 16/4/10.
 */
public class ZookeeperCreateApiSync implements Watcher {

    private static CountDownLatch cdl = new CountDownLatch(1);

    private static ZooKeeper zk;


    public static void main(String[] args) throws Exception{

        String connStr = "localhost:2181";

        zk = new ZooKeeper(connStr, 3000, new ZookeeperCreateApiSync());

        cdl.await();

        if (zk.exists("/zk-book", false) != null) {
            zk.delete("/zk-book", -1);
        }

        String path = zk.create("/zk-book", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("建立节点::" + path);

        String path2 = zk.create("/zk-book/c1", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("建立节点::" + path2);

        String path3 = zk.create("/zk-book/c2", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("建立节点::" + path3);

        List<String> children = zk.getChildren("/zk-book", true);
        System.out.println("子节点::" + children);


        String path4 = zk.create("/zk-book/c3", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("建立节点::" + path4);

        Stat stat = new Stat();
        System.out.println(new String(zk.getData("/zk-book/c3", true, stat)));
        System.out.println("cVersion: " + stat.getCversion() + ", cTime: " + stat.getCtime() + stat.getVersion());

        Stat stat1 = zk.setData("/zk-book/c3", "234".getBytes(), -1);
        System.out.println(stat1.getAversion() + "" + stat1.getVersion());

        Stat stat2 = zk.setData("/zk-book/c3", "345".getBytes(), stat1.getVersion());
        System.out.println("version: " + stat2.getVersion());

        zk.setData("/zk-book/c3", "457".getBytes(), stat1.getVersion());


        Thread.sleep(Integer.MAX_VALUE);
    }


    @Override
    public void process(WatchedEvent event) {

        System.out.println("收到watcher事件::" + event);

        if (Event.KeeperState.SyncConnected != event.getState()) {
            System.out.println("事件处理退出");
            return;
        }
        if (Event.EventType.None == event.getType() && null == event.getPath()) {
            System.out.println("连接建立: " + event.getPath());
            cdl.countDown();
        } else if (Event.EventType.NodeChildrenChanged == event.getType()) {
            System.out.println("子节点发生变更::" + event.getPath());

            try {
                List<String> children = zk.getChildren(event.getPath(), true, new Stat());
                System.out.println("变化后的子节点::" + children);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


    }
}
